package com.alibaba.sample.kantiezi.web.store.module.action;

import com.alibaba.biz.command.Command;
import com.alibaba.biz.command.CommandSupport;
import com.alibaba.biz.command.dispatcher.CommandDispatcher;
import com.alibaba.biz.command.result.Result;

import com.alibaba.common.lang.ArrayUtil;

import com.alibaba.sample.kantiezi.dal.dataobject.Cart;
import com.alibaba.sample.kantiezi.web.common.WebConstant;

import com.alibaba.service.form.Form;
import com.alibaba.service.form.FormService;
import com.alibaba.service.form.Group;
import com.alibaba.service.template.TemplateContext;

import com.alibaba.turbine.module.action.TemplateAction;
import com.alibaba.turbine.service.rundata.RunData;

import com.alibaba.webx.WebxException;

import java.util.Iterator;
import java.util.List;

public abstract class CartAction extends TemplateAction {
    protected abstract CommandDispatcher getCommandDispatcher();

    protected abstract FormService getFormService();

    public void doAddItem(RunData rundata, TemplateContext context)
            throws WebxException {
        Cart cart = (Cart) rundata.getSession().getAttribute(WebConstant.kantiezi_CART_KEY);

        if (cart == null) {
            cart = new Cart();
        }

        String itemId = rundata.getParameters().getString("itemId");

        cart.addItem(itemId);

        rundata.getSession().setAttribute(WebConstant.kantiezi_CART_KEY, cart);

        context.put("itemAdded", Boolean.TRUE);
    }

    public void doRemoveItem(RunData rundata, TemplateContext context)
            throws WebxException {
        Cart cart = (Cart) rundata.getSession().getAttribute(WebConstant.kantiezi_CART_KEY);

        if (cart == null) {
            cart = new Cart();
        }

        String itemId = rundata.getParameters().getString("itemId");

        cart.removeItem(itemId);

        rundata.getSession().setAttribute(WebConstant.kantiezi_CART_KEY, cart);
    }

    public void doUpdate(RunData rundata, TemplateContext context)
            throws WebxException {
        Cart cart = (Cart) rundata.getSession().getAttribute(WebConstant.kantiezi_CART_KEY);

        if (cart == null) {
            return;
        }

        Command command = new CommandSupport("viewCart");

        command.getParameters().put("cart", cart);

        Result result = getCommandDispatcher().execute(command);

        if (result.isSuccess()) {
            cart = (Cart) result.getDefaultModel();

            Form form = getFormService().getForm(rundata);

            if (form.isValid()) {
                List groups = form.getGroups();

                for (Iterator i = groups.iterator(); i.hasNext();) {
                    Group group = (Group) i.next();

                        String itemId        = group.getInstanceKey();
                        int    quantity      = group.getField("quantity").getIntegerValue();
                        int    stockQuantity = cart.getCartItem(itemId).getItem().getQuantity();

                        if (quantity > stockQuantity) {
                            group.getField("quantity").setMessage("outOfStock",
                                                                  ArrayUtil.toMap(new Object[][] {
                                                                                      {
                                                                                          "stockQuantity",
                                                                                          new Integer(stockQuantity)
                                                                                      }
                                                                                  }));
                        } else {
                            cart.setQuantity(itemId, quantity);
                        }
                    }
                
            }
        }

        rundata.getSession().setAttribute(WebConstant.kantiezi_CART_KEY, cart);
    }
}
